package com.kai.admin.util;

/**
 * <p>
 * 缓存 key 注册中心
 * </p>
 *
 * @author weijk
 * @since 2023/6/29
 */
public class CacheKeyRegistryUtil {

    /**
     * token 缓存 key
     */
    private static final String TOKEN_KEY = "login:token:";

    /**
     * 验证码缓存 key
     */
    private static final String CAPTCHA_KEY = "captcha:";

    /**
     * 获取 token 缓存 key
     *
     * @param token token
     * @return token 缓存 key
     */
    public static String getTokenKey(String token) {
        return newBuilder(64).append(TOKEN_KEY).append(token).toString();
    }

    /**
     * 获取验证码缓存 key
     *
     * @param key 标识
     * @return 验证码缓存 key
     */
    public static String getCaptchaKey(String key) {
        return newBuilder(64).append(CAPTCHA_KEY).append(key).toString();
    }

    /**
     * 统一用这个方法来new StringBuilder，必须预估字符串长度
     *
     * @param capacity 初始容量
     * @return StringBuilder
     */
    private static StringBuilder newBuilder(int capacity) {
        return new StringBuilder(capacity);
    }
}
